package cn.kgc.bdqn_studentinfomanager.service.UserService.impl;

import cn.kgc.bdqn_studentinfomanager.pojo.Model.BdqnTeacherinfo;
import cn.kgc.bdqn_studentinfomanager.service.UserService.TokenService;
import cn.kgc.bdqn_studentinfomanager.utils.MD5Util;
import cn.kgc.bdqn_studentinfomanager.utils.UserAgentUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * by Shanshui CC on 2020/12/8 8:46
 */
@Service("tokenService")
public class TokenServiceImpl implements TokenService {


    @Override
    public String generatorToken(String userAgent, BdqnTeacherinfo bdqnTeacherinfo) {

        if (StringUtils.isBlank(userAgent)) {
            return "用户标识丢失";
        }
        StringBuffer sb = new StringBuffer();

        sb.append("token:");
        //用户Id
        sb.append(bdqnTeacherinfo.getId() + "-");
        //用户职位标识
        sb.append(bdqnTeacherinfo.getRole()+ "-");
        //识别设备标识
        if (UserAgentUtil.CheckAgent(userAgent)) {
            //移动设备
            sb.append("MOBILE-");
        } else {
            //非移动设备
            sb.append("PC-");
        }
        sb.append(MD5Util.MD5EncodeUtf8(bdqnTeacherinfo.getPhone()) + "-");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        sb.append(simpleDateFormat.format(new Date()) + "-");
        sb.append(StringUtils.substring(MD5Util.MD5EncodeUtf8(userAgent), 0, 6));
        return sb.toString();
    }

    public static void main(String[] args) {

    }

}
